package net.csdn.business.kanban.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import net.csdn.business.kanban.api.entity.KanbanDetail;
import net.csdn.business.kanban.api.enums.BusinessTypeEnums;
import net.csdn.business.kanban.api.mapper.KanbanDetailMapper;
import net.csdn.business.kanban.api.service.KanbanDetailService;
import net.csdn.business.kanban.common.domain.request.MoveIssueParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @ClassName KanbanServiceImpl
 * @Description TODO
 * @Author liuzhisheng
 * @Date 2023/9/13
 **/
@Service
@RequiredArgsConstructor
public class KanbanDetailServiceImpl extends ServiceImpl<KanbanDetailMapper, KanbanDetail> implements KanbanDetailService {
    private final KanbanDetailMapper kanbanDetailMapper;

    @Override
    public List<String> getIssueIds(Long projectId, Integer businessType, String kanbanId) {
        return kanbanDetailMapper.getIssueIds(projectId, businessType, kanbanId);
    }

    @Override
    public boolean removeKanbanDetails(String kanbanId) {
        return kanbanDetailMapper.removeKanbanDetail(kanbanId) > 0 ? true : false;
    }

    @Override
    public boolean deleteKanbanDetail(String id) {
        QueryWrapper<KanbanDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(KanbanDetail::getObjectId,id);
        return  kanbanDetailMapper.delete(queryWrapper) > 0 ? true : false;
    }

    @Override
    public List<KanbanDetail> kanbanDetailsList(String kanbanId,Long limitNum,Boolean archivedTask) {
        QueryWrapper<KanbanDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(KanbanDetail::getKanbanId,kanbanId);
        if(archivedTask != null){
            queryWrapper.lambda().eq(KanbanDetail::getArchivedTask,archivedTask);
        }
        queryWrapper.last(" limit "+limitNum);
        return kanbanDetailMapper.selectList(queryWrapper);
    }

    @Override
    public boolean archived(String objectId, boolean archivedTask) {
        UpdateWrapper<KanbanDetail> updateWrapper = new UpdateWrapper();
        updateWrapper.lambda().eq(KanbanDetail::getObjectId,objectId).set(KanbanDetail::getArchivedTask,archivedTask);
        return kanbanDetailMapper.update(null,updateWrapper) > 0 ? true : false;
    }

    @Override
    public KanbanDetail getKanbanDetailByObjectId(String objectId) {
        QueryWrapper<KanbanDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(KanbanDetail::getObjectId,objectId);
        return kanbanDetailMapper.selectOne(queryWrapper);
    }

    @Override
    public List<KanbanDetail> getKanbanDetail(Integer businessType, List<String> businessIds) {
        QueryWrapper<KanbanDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(KanbanDetail::getBusinessType,businessType);
        queryWrapper.lambda().in(KanbanDetail::getBusinessId,businessIds);
        return kanbanDetailMapper.selectList(queryWrapper);
    }

    @Override
    public long countRow(String objectId) {
        QueryWrapper<KanbanDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(KanbanDetail::getObjectId,objectId);
        return kanbanDetailMapper.selectCount(queryWrapper);
    }

    @Override
    public int removeFieldContentJson(String kanbanId, Long fieldId) {
        return kanbanDetailMapper.removeFieldContentJson(kanbanId, fieldId);
    }

    @Override
    public boolean isBatchExist(String kanbanId,List<String> issueIds) {
        QueryWrapper<KanbanDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(KanbanDetail::getKanbanId,kanbanId).eq(KanbanDetail::getBusinessType, BusinessTypeEnums.ISSUE.getCode()).in(KanbanDetail::getBusinessId,issueIds);
        return kanbanDetailMapper.selectCount(queryWrapper) > 0 ? true : false;
    }

    @Override
    public void updateTitle(Long id, String filedValue) {
        kanbanDetailMapper.updateTitle(id,filedValue);
    }

    @Override
    public void updateAssignee(Long id, String filedValue) {
        kanbanDetailMapper.updateAssignee(id,filedValue);
    }

    @Override
    public void updateLabels(Long id, String filedValue) {
        UpdateWrapper<KanbanDetail> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().set(KanbanDetail::getLabels,filedValue).eq(KanbanDetail::getId,id);
        kanbanDetailMapper.update(null,updateWrapper);
    }

    @Override
    public void updateMilepost(Long id, String filedValue) {
        kanbanDetailMapper.updateMilepost(id,filedValue);
    }

    @Override
    public void updateFieldContentJson(Long id, String filedValue) {
        UpdateWrapper<KanbanDetail> wrapper = new UpdateWrapper();
        wrapper.lambda().set(KanbanDetail::getFieldContentJson,filedValue).eq(KanbanDetail::getId,id);
        kanbanDetailMapper.update(null,wrapper);
    }

    @Override
    public void removeIssue(String issueId) {
        QueryWrapper<KanbanDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(KanbanDetail::getBusinessType,BusinessTypeEnums.ISSUE.getCode()).eq(KanbanDetail::getBusinessId,issueId);
        kanbanDetailMapper.delete(queryWrapper);
    }

    @Override
    public List<KanbanDetail> queryIssueDetailList(String issueId) {
        QueryWrapper<KanbanDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(KanbanDetail::getBusinessType,BusinessTypeEnums.ISSUE.getCode()).eq(KanbanDetail::getBusinessId,issueId);
        return kanbanDetailMapper.selectList(queryWrapper);
    }

    @Override
    public int batchUpdateIssueDetail(List<Long> ids, MoveIssueParam param) {
        return kanbanDetailMapper.batchUpdateIssueDetail(ids,param);
    }
}
